<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/6/8
 * Time: 17:40
 */

namespace app\applet\controller;


use app\share\model\Token;
use think\Db;
use think\Validate;

class MyOrder
{

    public function __construct()
    {
        $this->order=new \app\applet\model\MyOrder();
        $this->token=new Token();
    }

    /**
     * 我的订单列表数据   type  1=全部  2=待发货  3=待收货  4=待评价 5=退换货 6=已取消 12已完成
     */
    public function orderList(){
        $param=input();
        $member_id = get_header_id("token");
        $page=empty($param['current_page']) ? 1 : $param['current_page'];
        $page_size=empty($param['page_num']) ? 10: $param['page_num'];
        $type=empty($param['type']) ? 1: $param['type'];
        //$member_id=18;
        $returnData=$this->order->orderList($member_id,$type,$page,$page_size);
        returnJson(200,'success',$returnData);
    }

    /**
     * 我的订单详情    付款成功的跳转到支付成功的页面   未付款的跳转到取消订单页面
     *      type 1=付款成功  2=未付款
     */
    public function orderDetail(){
        $member_id = get_header_id("token");
        $order_id=input('order_id');
        //$member_id=18;

        $returnData=$this->order->orderDetail($member_id,$order_id);
        returnJson(200,'success',$returnData);

    }

    /**
     * 删除订单
     */
    public function orderDel()
    {
        $member_id = get_header_id("token");
        $order_id = input('order_id');
        $data['status']=7;
        $data['is_del']=2;
        $data['update_time']=time();

        $this->order->updateOrder($order_id,$member_id,$data,1);

        returnJson(200,'操作成功');

    }

    /**
     * 订单评价页面数据     单个商品评价，一个订单可能有多个商品，可能评价多次
     */
    public function orderComment(){
        $member_id = get_header_id("token");
        $order_id=input('order_id');
        $order_goods_id=input('order_goods_id');


        $returnData=$this->order->selectOrderOne($order_id,$member_id,1,$order_goods_id);

        returnJson(200,'success',$returnData);
    }

    /**
     * 订单评价提交
     */
    public function submitEvaluate(){
        $param=input();
        $validate = new Validate([
            ['order_id', 'require', '订单id不能为空'],
            ['order_goods_id', 'require', '订单商品数据id不能为空'],
            ['star', 'require', '星级评价不能为空'],
            ['content', 'require', '评价内容不能为空'],
        ]);
        if(!$validate->check($param))
        {
            returnJson(-2003,$validate->getError());
        }
        $member_id = get_header_id("token");

        $this->order->comment($member_id,$param);

        returnJson(200,'提交成功');

    }

    /**
     * 订单评价详情
     */
    public function orderEvaluate(){
        $param=input();
        $validate = new Validate([
            ['order_id', 'require', '订单id不能为空'],
            ['goods_id', 'require', '订单商品数据id不能为空'],
        ]);
        if(!$validate->check($param))
        {
            returnJson(-2003,$validate->getError());
        }
        $member_id = get_header_id("token");
        //$member_id=5;

        $current_page = empty($param['current_page']) ? 1 : $param['current_page'];
        $page_num = empty($param['page_num']) ? 10: $param['page_num'];

        $commentList=$this->order->commentDetail($member_id,$param,$current_page,$page_num);

        returnJson(200,'获取成功',$commentList);

    }

    /**
     * 确认收货
     */
    public function orderConfirm(){
        $member_id = get_header_id("token");
        $input=input();
        $order_id = input('order_id');
        $data['status']=4;
        $data['update_time']=time();
        if(!empty($input['spec_id'])){
            $data['spec_id']=$input['spec_id'];
        }
        //$member_id =19;
        $this->order->updateOrder($order_id,$member_id,$data,2);
        returnJson(200,'操作成功');
    }


    /**
     *   提醒发货（给商家发送一条消息）
     */
    public function remindShipping(){
        $member_id = get_header_id("token");
        $order_id = input('order_id');

        $this->order->remind($member_id,$order_id);
        returnJson(200,'操作成功');
    }


    /**
     *   -------------------------------查看物流--------------------------------------------
     */

    /**
     *   -------------------------------售后详情页面（有一个商家处理时间）--------------------------------------------
     */
    public function afterDetail(){
        $member_id = get_header_id("token");
        $order_id = input('order_id');

        $data=$this->order->afterDetail($member_id,$order_id);
        returnJson(200,'success',$data);

    }

    /**
     * 售后取消退款
     */
    public function afterRefund(){
        $member_id = get_header_id("token");
        $order_id = input('order_id');
        $order_goods_id = input('order_goods_id');
        //$member_id=1;

        $this->order->afterRefund($member_id,$order_id,$order_goods_id);
        returnJson(200,'操作成功');

    }

    /**
     * 申请售后页面    一个订单商品只申请一次
     */
    public function afterSales(){
        $member_id = get_header_id("token");
        $order_id=input('order_id');
        $order_goods_id=input('order_goods_id');
        //$member_id=35;

        $returnData=$this->order->selectOrderOne($order_id,$member_id,2,$order_goods_id);


        returnJson(200,'success',$returnData);
    }

    /**
     * 售后提交
     */
    public function afterSalesSubmit(){
        $param=input();
        $validate = new Validate([
            ['order_id', 'require', '订单id不能为空'],
            ['order_goods_id', 'require', '订单商品数据id不能为空'],
            ['after_type', 'require', '售后类型不能为空'],
            ['after_reason', 'require', '售后原因不能为空'],
            ['spec_id', 'require', '规格id不能为空'],
        ]);
        if(!$validate->check($param))
        {
            returnJson(-2003,$validate->getError());
        }
        $member_id = get_header_id("token");
        //$member_id=18;

        $this->order->afterSalesPost($member_id,$param);

        returnJson(200,'提交成功');
    }


    /**
     * 写入数据，测试
     */
    public function addOrder(){
        $member_id=8;
        $data=Db::name('goods')->where('goods_id',5)->find();
        $data2=Db::name('goods_spec')->where('goods_id',5)->select();

        dump($data);
        dump($data2);
    }

    public function test(){
        $member_id=8;
        $goods_id=12;
        $store_id=70;
        $order_sn=get_order1(1,1,$member_id);

        $data['member_id']=$member_id;
        $data['store_id']=$store_id;
        $data['order_sn']=$order_sn;
        $data['wx_money']='0.01';
        $data['order_score_money']='30';
        $data['order_coin_money']='90';
        $data['receiver']='豆皮';
        $data['re_phone']='17012345678';
        $data['address']='四川省成都市武侯区';
        $data['create_time']=time();

        $order_id=Db::name('order')->insertGetId($data);

        $info['order_id']=$order_id;
        $info['goods_id']=$goods_id;
        $info['goods_num']=1;
        $info['score_money']='30';
        $info['coin_money']='90';
        $info['spec_name']='黑白格子';
        $info['goods_name']='简易座椅';
        $info['delivery_fee']='20';
        $info['create_time']=time();

        Db::name('order_goods')->insert($info);


    }


}